<template>
  <div class="todo-container">
    <div class="todo-wrap">
      <myheader :addtodo="addtodo"></myheader>
      <mylist
        :todolist="todolist"
        :updatachange="updatachange"
        :deletetodo="deletetodo"
      ></mylist>
      <myfooter
        :todolist="todolist"
        :checkAllTodo="checkAllTodo"
        :clearAllTodo="clearAllTodo"
      ></myfooter>
    </div>
  </div>
</template>

<script>
import myheader from "./components/myheader.vue";
import mylist from "./components/mylist.vue";
import myfooter from "./components/myfooter.vue";

export default {
  name: "App",
  components: {
    myheader,
    mylist,
    myfooter,
  },
  data() {
    return {
      todolist:JSON.parse(localStorage.getItem("todos"))||[],
    };
  },
  methods: {
    // 添加todo
    addtodo(x) {
      this.todolist.unshift(x);
    },
    //修改item的选择框
    updatachange(id) {
      this.todolist.forEach((todo) => {
        if (todo.id === id) todo.done = !todo.done;
      });
    },
    // 删除item
    deletetodo(id) {
      this.todolist = this.todolist.filter((todo) => {
        return todo.id !== id;
      });
    },
    // 全选或者取消全选
    checkAllTodo(done) {
      this.todolist.forEach((todo) => {
        todo.done = done;
      });
    },
    // 清除所有已经完成
    clearAllTodo() {
      this.todolist = this.todolist.filter((todo) => {
        return !todo.done;
      });
    },
  },
  watch: {
    todolist: {
      deep: true,
      handler(value) {
        localStorage.setItem("todos", JSON.stringify(value));
      },
    },
  },
};
</script>

<style>
body {
  background: #fff;
}

.btn {
  display: inline-block;
  padding: 4px 12px;
  margin-bottom: 0;
  font-size: 14px;
  line-height: 20px;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2),
    0 1px 2px rgba(0, 0, 0, 0.05);
  border-radius: 4px;
}

.btn-danger {
  color: #fff;
  background-color: #da4f49;
  border: 1px solid #bd362f;
}

.btn-danger:hover {
  color: #fff;
  background-color: #bd362f;
}

.btn:focus {
  outline: none;
}

.todo-container {
  width: 600px;
  margin: 0 auto;
}
.todo-container .todo-wrap {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}
</style>
